#!/usr/bin/env bash

file="${BASH_SOURCE[0]}"
base_dir="$(cd "$(dirname "$file")/" && pwd)"

# Build DJVM module and CLI
cd "$base_dir/.."
if !(../gradlew shadowJar); then
    echo "Failed to build DJVM"
    exit 1
fi

djvm_cli_jar=$(ls -1 $base_dir/../cli/build/libs/corda-djvm-cli-*.jar)

# Generate auto-completion file for Bash and ZSH
cd "$base_dir"
if !(java -cp $djvm_cli_jar \
    picocli.AutoComplete -n djvm net.corda.djvm.tools.cli.Commands -f); then
    echo "Failed to generate auto-completion file"
    exit 1
fi

# Create a symbolic link to the `djvm` utility
sudo ln -sf "$base_dir/djvm" /usr/local/bin/djvm
